package com.evan.review.architecture.components;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;

import com.evan.review.R;
import com.evan.review.architecture.components.room.AppDatabase;
import com.evan.review.architecture.components.room.Note;
import com.evan.review.architecture.components.room.NoteDao;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Room示例Activity
 * 展示Room数据库的基本用法
 */
public class RoomExampleActivity extends AppCompatActivity {
    
    private EditText etNoteTitle;
    private EditText etNoteContent;
    private TextView tvNotes;
    
    private AppDatabase db;
    private NoteDao noteDao;
    private ExecutorService executorService;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_room_example);
        
        // 设置标题
        setTitle("Room示例");
        
        // 初始化数据库
        db = Room.databaseBuilder(getApplicationContext(),
                AppDatabase.class, "notes-db")
                .build();
        
        noteDao = db.noteDao();
        
        // 创建线程池执行数据库操作
        executorService = Executors.newFixedThreadPool(4);
        
        // 初始化视图
        initViews();
        
        // 加载笔记
        loadNotes();
    }
    
    private void initViews() {
        etNoteTitle = findViewById(R.id.et_note_title);
        etNoteContent = findViewById(R.id.et_note_content);
        tvNotes = findViewById(R.id.tv_notes);
        
        // 添加笔记按钮
        Button btnAddNote = findViewById(R.id.btn_add_note);
        btnAddNote.setOnClickListener(v -> {
            addNote();
        });
        
        // 清空笔记按钮
        Button btnClearNotes = findViewById(R.id.btn_clear_notes);
        btnClearNotes.setOnClickListener(v -> {
            clearNotes();
        });
    }
    
    /**
     * 加载所有笔记
     */
    private void loadNotes() {
        executorService.execute(() -> {
            // 在后台线程中查询数据库
            List<Note> notes = noteDao.getAll();
            
            // 在UI线程中更新UI
            runOnUiThread(() -> {
                if (notes.isEmpty()) {
                    tvNotes.setText("暂无笔记");
                } else {
                    StringBuilder sb = new StringBuilder();
                    for (Note note : notes) {
                        sb.append("ID: ").append(note.getId())
                                .append("\n标题: ").append(note.getTitle())
                                .append("\n内容: ").append(note.getContent())
                                .append("\n\n");
                    }
                    tvNotes.setText(sb.toString());
                }
            });
        });
    }
    
    /**
     * 添加笔记
     */
    private void addNote() {
        String title = etNoteTitle.getText().toString().trim();
        String content = etNoteContent.getText().toString().trim();
        
        if (title.isEmpty() || content.isEmpty()) {
            Toast.makeText(this, "标题和内容不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        
        // 创建笔记对象
        Note note = new Note();
        note.setTitle(title);
        note.setContent(content);
        
        // 在后台线程中插入数据
        executorService.execute(() -> {
            // 插入笔记
            noteDao.insert(note);
            
            // 在UI线程中更新UI
            runOnUiThread(() -> {
                Toast.makeText(RoomExampleActivity.this, 
                        "笔记已添加", Toast.LENGTH_SHORT).show();
                
                // 清空输入框
                etNoteTitle.setText("");
                etNoteContent.setText("");
                
                // 重新加载笔记
                loadNotes();
            });
        });
    }
    
    /**
     * 清空所有笔记
     */
    private void clearNotes() {
        executorService.execute(() -> {
            // 在后台线程中删除所有笔记
            noteDao.deleteAll();
            
            // 在UI线程中更新UI
            runOnUiThread(() -> {
                Toast.makeText(RoomExampleActivity.this, 
                        "所有笔记已删除", Toast.LENGTH_SHORT).show();
                
                // 重新加载笔记
                loadNotes();
            });
        });
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 关闭线程池
        executorService.shutdown();
    }
} 